package demo3;

class Person{
    public String name;
    public int age;

    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
            if (obj == null) return false ;
            if(this == obj) return true ;
            // 不是Person类对象
            if (!(obj instanceof Person)) return false ;
        Person person = (Person) obj ; // 向下转型，比较属性值
        return this.name.equals(person.name) && this.age==person.age ;
    }
}

public class Test {
    public static void main(String[] args) {
        Person person1=new Person("张三",32);
        Person person2=new Person("张三",32);
        Person person3=person1;
        System.out.println(person1);

        System.out.println(person1==person2);//false--比较的是地址
        System.out.println(person1.equals(person2));//true
        System.out.println(person1.equals(person3));//true
       /* public boolean equals(Object obj){
            return(this==obj);//person1==person2
        }*/

        //说明上述两段代码并无不同，那我们只好重写equals()
    }
}
